
import GraphicsLayer from "@geoscene/core/layers/GraphicsLayer";
import SketchViewModel from "@geoscene/core/widgets/Sketch/SketchViewModel";
import * as webMercatorUtils from "@geoscene/core/geometry/support/webMercatorUtils.js";

import { editSymbol } from './symbol';

export default class EditTool{
    appConfig: any = null;
    tempLayer: GraphicsLayer = new GraphicsLayer({
            id: "editTempLayer"
        });
    sketchViewModel: SketchViewModel;

    constructor(options={config:{}}){
        this.appConfig = options.config;
        this.appConfig.activeView.map.add(this.tempLayer);
        this.sketchViewModel = this.createSketchViewModel();
    }

    webmercatorToWgs84(geo:any){
        return webMercatorUtils.webMercatorToGeographic(geo);
    }
    
    Wgs84Towebmercator(geo:any){
        return webMercatorUtils.geographicToWebMercator(geo);
    }
    
    
    getEditSymbolByType(geoType:string,EditType:string){
        let nSymbol = null,highSymbol = null,createSymbol = null;
        switch(geoType){
            case 'point':
                nSymbol = editSymbol.ptnSymbol;
                highSymbol = editSymbol.pthighSymbol;
                createSymbol = editSymbol.ptcreateSymbol;
                break;
            case 'polyline':
                nSymbol = editSymbol.plnSymbol;
                highSymbol = editSymbol.plhighSymbol;
                createSymbol = editSymbol.plcreateSymbol;
                break;
            case 'polygon':
                nSymbol = editSymbol.pgnSymbol;
                highSymbol = editSymbol.pghighSymbol;
                createSymbol = editSymbol.pgcreateSymbol;
                break;
            default:
                break;
        }

        if(EditType == 'init'){
            return nSymbol;
        }else if(EditType == 'highlight'){
            return highSymbol;
        }else if(EditType == 'create'){
            return createSymbol;
        }
        
    }

    
    createSketchViewModel(){
        let that = this;
        return new SketchViewModel({//创建绘图对象
            view: this.appConfig.activeView,//绑定视图
            layer: that.tempLayer,//绑定临时图层
            activeFillSymbol: editSymbol.pgcreateSymbol,//激活状态样式
            polygonSymbol: editSymbol.pgcreateSymbol,//创建完成后的面要素样式
            pointSymbol:editSymbol.ptcreateSymbol,
            polylineSymbol:editSymbol.plcreateSymbol,
            updateOnGraphicClick:false,
            defaultUpdateOptions: {//默认编辑参数
              toggleToolOnClick: false,
              enableRotation: false,//旋转
              enableScaling: false,//按比例缩放
              tool:'reshape'
            //   toggleToolOnClick:false
            },
            snappingOptions: {//捕捉参数
              enabled: true,
              selfEnabled: true,
              featureEnabled: true,
              featureSources: [{layer: that.tempLayer}]
            }
          });
    }

    
    createFeature(ftype:any,manner:any){
        let mode = manner||'clcik';
        this.sketchViewModel.create(ftype, {mode: mode})//绘制面
    }

    
    updateFeature(graphic:any,type:any){
        let option:any = null;
        if(type){
            option = {//默认编辑参数
                toggleToolOnClick: false,
                enableRotation: false,//旋转
                enableScaling: false,//按比例缩放
                tool:type
              //   toggleToolOnClick:false
              }
        }
        this.sketchViewModel.update(graphic,option);
    }

    
    undo(){
        this.sketchViewModel.undo();
    }

    
    redo(){
        this.sketchViewModel.redo();
    }

    
    cancel(){
        this.sketchViewModel.cancel();
    }

    
    canRedo(){
        return this.sketchViewModel.canRedo();
    }

    
    canUndo(){
        return this.sketchViewModel.canUndo();
    }

    
    complete(){
        this.sketchViewModel.complete();
    }

    
    destroy(){
        this.sketchViewModel.destroy();
    }

}